package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 查询今日运营数据
     * @param begin
     * @param end
     * @return
     */
    @Override
    public BusinessDataVO getbusinessData(LocalDateTime begin, LocalDateTime end) {

        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        //获取今天订单总数
        Integer orderCount = orderMapper.getOrderCount(map);
        //获取新增用户数newUsers
        Integer newUser = userMapper.getUserCount(map);
        map.put("status", Orders.COMPLETED);
        //获取今天营业额
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null? 0.0 : turnover;
        //获取今天有效订单数validOrderCount
        Integer validOrderCount = orderMapper.getOrderCount(map);
        Double orderCompletionRate = 0.0;
        Double unitPrice = 0.0;
        if (validOrderCount != 0){
            //获取订单完成率
             orderCompletionRate = validOrderCount.doubleValue() / orderCount;
            //获取平均客单价unitPrice
             unitPrice = turnover / validOrderCount;
        }


        return BusinessDataVO.builder()
                .newUsers(newUser)
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .build();
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOver() {
        Integer status = StatusConstant.ENABLE;
        //获取在售中套餐数量
        Integer sold = setmealMapper.getSetmealCount(status);
        //获取停售套餐数量
        status = StatusConstant.DISABLE;
        Integer discontinued = setmealMapper.getSetmealCount(status);
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getOverviewDishes() {
        //获取在售中菜品数量
        Integer status = StatusConstant.ENABLE;
        Integer sold = dishMapper.getDishCount(status);
        //获取停售的菜品数量
        status = StatusConstant.DISABLE;
        Integer discontinued = dishMapper.getDishCount(status);
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询今日订单管理数据
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderOverViewVO getOverviewOrder(LocalDateTime begin, LocalDateTime end) {
        //待接单：waitingOrders 带派送：deliveredOrders 已完成：completedOrders 已取消：cancelledOrders 所有：allOrders
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        //获取今日所有订单数 allOrders
        Integer allOrders = orderMapper.getOrderCount(map);
        //获取待接单订单数 waitingOrders
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.getOrderCount(map);
        //获取待派送订单数 deliveredOrders
        map.put("status",Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.getOrderCount(map);
        //获取已完成订单数 completedOrders
        map.put("status",Orders.COMPLETED);
        Integer completedOrders = orderMapper.getOrderCount(map);
        //获取已取消订单数 cancelledOrders
        map.put("status",Orders.CANCELLED);
        Integer  cancelledOrders= orderMapper.getOrderCount(map);
        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }
}
